package com.bonc.hbase.api.client;


import java.io.IOException;

import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.springframework.stereotype.Component;


/**
 * @author caiqiang
 * @version 2016年7月28日
 * @see HbaseOperateClient
 * @since
 */
@Component(value = "hbaseOperateClient")
public class HbaseOperateClient {
    /**
     * Description: 单行查询
     * 
     * @param rowkey
     * @param tableName
     * @return Result
     * @throws IOException
     * @see
     */
    public Result getResult(byte[] rowkey, String tableName)
        throws IOException {

        Get get = new Get(rowkey);
        HTableInterface table = HBaseClient.getHConnection().getTable(tableName);
        Result result = table.get(get);

        table.close();
        return result;
    }

    /**
     * Description: 单元格查询
     * 
     * @param rowkey
     * @param tableName
     * @param family
     * @param qualifier
     * @return Result
     * @throws IOException
     * @see
     */
    public Result getResult(byte[] rowkey, String tableName, byte[] family, byte[] qualifier)
        throws IOException {

        Get get = new Get(rowkey);
        get.addColumn(family, qualifier);
        HTableInterface table = HBaseClient.getHConnection().getTable(tableName);
        Result result = table.get(get);

        table.close();
        return result;
    }

    /**
     * Description:范围查询
     * 
     * @param startRowkey
     * @param endRowkey
     * @param tableName
     * @return ResultScanner
     * @throws IOException
     *             ResultScanner
     * @see
     */
    public ResultScanner getResultScanner(byte[] startRowkey, byte[] endRowkey, String tableName)
        throws IOException {

        Scan scan = new Scan(startRowkey, endRowkey);
        scan.setSmall(true);
        HTableInterface table = HBaseClient.getHConnection().getTable(tableName);
        ResultScanner rs = table.getScanner(scan);

        table.close();
        return rs;
    }

}
